<?php
//自动生成,请根据需要修改
namespace app\message\controller;

use app\common\controller\Base;
use think\Db;
use think\db\Where;

/**
 * @title 站内信页面（模块11-01）
 * @description 站内信说明
 * @group 站内信（11）
 * @header name:model_id require:0 default:11-01 desc:模块
 * @groupremark 已使用错误码：1-11-01-0001 校验失败 1-11-01-0002 添加失败 1-11-01-0003 编辑校验失败 1-11-01-0004 编辑失败 1-11-01-0005 获取详情失败  1-11-01-0006 获取列表失败 1-11-01-0007获取单个详情失败 1-11-01-0008 删除失败
 */
class Message extends Base
{
    //测试
    public function test()
    {
        return 'test 111';
    }

    /**
     * @title 添加站内信
     * @description 添加站内信
     * @author 李锐志
     * @url /message/message/add
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:send_user_id type:int require:0 other: desc:发信人id
     * @param name:receive_user_id type:int require:0 other: desc:收信人id
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:anchor type:tinyint require:0 other: desc:锚点
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:站内信@!
     * @data    id:id    send_user_id:发信人id    receive_user_id:收信人id    content:内容    create_time:创建时间    topic_id:帖子id    anchor:锚点    status:状态
     * @return error_id:1-11-01-0001 校验失败  1-11-01-0002 添加失败
     */
    public function add()
    {
        $data = input();
        $data['create_time'] = time();
        /**
         *$validate = new \app\message\validate\Message();
         *$validate->scene('add');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-11-01-0001';
         *return $this->errorJson($msg);
         *}
         **/
        $model = new \app\message\model\Message();
        $flag = $model->save($data);
        if ($flag) {
            $msg['msg'] = '添加成功';
            $msg['data'] = $model->get($model['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-11-01-0002';
            $msg['msg'] = '添加失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 编辑站内信
     * @description 编辑站内信
     * @author 李锐志
     * @url /message/message/edit
     * @method *
     *
     * @header name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:send_user_id type:int require:0 other: desc:发信人id
     * @param name:receive_user_id type:int require:0 other: desc:收信人id
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:anchor type:tinyint require:0 other: desc:锚点
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:站内信@!
     * @data    id:id    send_user_id:发信人id    receive_user_id:收信人id    content:内容    create_time:创建时间    topic_id:帖子id    anchor:锚点    status:状态
     * @return error_id:1-11-01-0003 校验失败 1-11-01-0004 修改失败
     */
    public function edit()
    {
        $data = input();
        $validate = new \app\message\validate\Message();
        $validate->scene('edit');
        $result = $validate->batch(true)->check($data);
        if ($result !== true) {
            $msg['msg'] = '校验失败';
            $msg['validate'] = $validate->getError();
            $msg['error_id'] = '1-11-01-0003';
            return $this->errorJson($msg);
        }
        $model = new \app\message\model\Message();
        $flag = $model->edit($data);
        if ($flag !== false) {
            $msg['msg'] = '修改成功';
            $msg['data'] = $model->get($data['id']);
            return $this->successJson($msg);
        } else {
            $msg['error_id'] = '1-11-01-0004';
            $msg['msg'] = '修改失败';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 删除站内信
     * @description 删除站内信
     * @author 李锐志
     * @url /message/message/delete
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int|array require:1 other: desc:要删除的id
     * @return error_id:1-11-01-0008 删除失败
     */
    public function delete()
    {
        $id = input('id');
        $model = new \app\message\model\Message();
        $flag = $model->destroy($id);
        if ($flag) {
            $msg['msg'] = '删除成功';
            return $this->successJson($msg);
        } else {
            $msg['msg'] = '删除失败';
            $msg['error_id'] = '1-11-01-0008';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 单个查询站内信
     * @description 单个查询站内信
     * @author 李锐志
     * @url /message/message/info
     * @method *
     *
     * @param name:token require:1 default: desc:token
     *
     * @param name:id type:int require:0 other: desc:id
     * @param name:send_user_id type:int require:0 other: desc:发信人id
     * @param name:receive_user_id type:int require:0 other: desc:收信人id
     * @param name:content type:text require:0 other: desc:内容
     * @param name:create_time type:int require:0 other: desc:创建时间
     * @param name:topic_id type:int require:0 other: desc:帖子id
     * @param name:anchor type:tinyint require:0 other: desc:锚点
     * @param name:status type:tinyint require:0 other: desc:状态
     * @return data:站内信@!
     * @data    id:id    send_user_id:发信人id    receive_user_id:收信人id    content:内容    create_time:创建时间    topic_id:帖子id    anchor:锚点    status:状态
     * @return error_id:1-11-01-0005 校验失败 1-11-01-0007 获取失败
     */
    public function info()
    {
        $model = new \app\message\model\Message();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        /**
         *$validate = new \app\message\validate\Message();
         *$validate->scene('info');
         *$result = $validate->batch(true)->check($data);
         */
        if (!$data) {
            $msg['msg'] = '必须有参数';
            $msg['error_id'] = '1-11-01-0005';
            return $this->errorJson($msg);
        }
        $model = new \app\message\model\Message();
        $info = $model->where($data)->find();
        if ($info) {
            $msg['msg'] = '获取成功';
            $msg['data'] = $info;
            return $this->successJson($msg);
        } else {

            $msg['msg'] = '获取失败';
            $msg['error_id'] = '1-11-01-0007';
            return $this->errorJson($msg);
        }
    }

    /**
     * @title 查询列表站内信
     * @description 查询列表站内信
     * @author 李锐志
     * @url /message/message/get_list
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     * @return list:站内信@
     * @list    id:id    send_user_id:发信人id    receive_user_id:收信人id    content:内容    create_time:创建时间    topic_id:帖子id    anchor:锚点    status:状态
     * @return error_id:1-11-01-0006 校验失败
     */
    public function get_list()
    {
        $model = new \app\message\model\Message();
        $Fields = $model->getTableFields();
        $data = \think\facade\Request::only($Fields);
        foreach ($data as $k => $v) {
            if ($v === 0) {
                continue;
            }
            if ($v == null) {
                unset($data[$k]);
            }
        }
        if (isset($data['send_user_id']) && $data['send_user_id'] && !empty($data['send_user_id'])) {
            $user_map['nickname'] = array('like', "%{$data['send_user_id']}%");//用户名
            $user_ids = Db::table('zb_user.tp_user u')->where(new Where($user_map))->column('id');
            $data['send_user_id'] = array('in', $user_ids);
        }
        if (isset($data['receive_user_id']) && $data['receive_user_id'] && !empty($data['receive_user_id'])) {
            $user_map['nickname'] = array('like', "%{$data['receive_user_id']}%");//用户名
            $user_ids = Db::table('zb_user.tp_user u')->where(new Where($user_map))->column('id');
            $data['receive_user_id'] = array('in', $user_ids);
        }
        if (isset($data['topic_id']) && $data['topic_id'] && !empty($data['topic_id'])) {
            $topic_map['title'] = array('like', "%{$data['topic_id']}%");//用户名
            $topic_ids = Db::table('zb_topic.tp_topic t')->where(new Where($topic_map))->column('id');
            $data['topic_id'] = array('in', $topic_ids);
        }
        if (isset($data['status']) && $data['status'] && !empty($data['status'])) {
            $data['status'] = ['=', "{$data['status']}"];
        }
        /**
         *$validate = new \app\message\validate\Message();
         *$validate->scene('lists');
         *$result = $validate->batch(true)->check($data);
         *if ($result !== true) {
         *$msg['msg']='校验失败';
         *$msg['validate']=$validate->getError();
         *$msg['error_id']='1-11-01-0006';
         *return $this->errorJson($msg);
         *}
         **/
        $data = new Where($data);
        $list = $model->where($data)->order('id', 'desc')->paginate();
        $user_list = Db::table('zb_user.tp_user')->field('id as uid,nickname')->select();
        $topic_title = Db::table('zb_topic.tp_topic')->field('id as tid,title')->select();
        foreach ($list as $k => $v) {
            foreach ($user_list as $kk => $vv) {
                if ($v['send_user_id'] == $vv['uid']) {
                    $list[$k]['send_user'] = $user_list[$kk]['nickname'];
                }
            }
        }
        foreach ($list as $k => $v) {
            foreach ($user_list as $kk => $vv) {
                if ($v['receive_user_id'] == $vv['uid']) {
                    $list[$k]['receive_user'] = $user_list[$kk]['nickname'];
                }
            }
        }
        foreach ($list as $k => $v) {
            foreach ($topic_title as $kk => $vv) {
                if ($v['topic_id'] == $vv['tid']) {
                    $list[$k]['title'] = $topic_title[$kk]['title'];
                }
            }
        }
        $msg['list'] = $list;
        return $this->successJson($msg);
    }

    /**
     * 后台站内信已读按钮
     */
    public function invoke()
    {
        $data = input();

        Db::name('message')
            ->where('id', $data['id'])
            ->setField('status', 1);

        $msg['msg'] = '站内信已读成功';
        return $this->successJson($msg);
    }

    /**
     * 后台站内信未读按钮
     */
    public function forbidden()
    {
        $data = input();

        Db::name('message')
            ->where('id', $data['id'])
            ->setField('status', 0);

        $msg['msg'] = '站内信未读成功';
        return $this->successJson($msg);
    }

    /**
     * @title 前台通过token获取未阅读站内信数量
     * @description 前台通过token获取未阅读站内信数量
     * @author 李锐志
     * @url /message/message/token_get_message
     * @method *
     * @param name:token require:1 default: desc:token
     */
    public function token_get_message()
    {
        $result = Db::name('message')
            ->where('receive_user_id', USERID)
            ->where('status', 0)
            ->count();

        $msg['msg'] = '站内信数量获取成功';
        $msg['quantity'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台通过id获取未阅读站内信数量
     * @description 前台通过id获取未阅读站内信数量
     * @author 李锐志
     * @url /message/message/id_get_message
     * @method *
     * @param name:user_id require:1 default: desc:用户id
     */
    public function id_get_message()
    {
        $result = Db::name('message')
            ->where('receive_user_id', input('user_id'))
            ->where('status', 0)
            ->count();

        $msg['msg'] = '站内信数量获取成功';
        $msg['quantity'] = $result;
        return $this->successJson($msg);
    }

    /**
     * @title 前台获取站内信页面内容
     * @description 前台获取站内信页面内容
     * @author 李锐志
     * @url /message/message/token_get_message_quantity
     * @method *
     * @param name:token require:1 default: desc:token
     * @param name:page require:1 default: desc:第几页
     * @param name:pagesize require:1 default: desc:每页数
     */
    public function token_get_message_quantity()
    {
        $result = Db::name('message')
            ->where('receive_user_id', USERID)
            ->order('id', 'desc')
            ->withAttr('topic_flag', function ($value, $data) {
//                $topic = get_curl('topic','topic/info',['id'=>$data['topic_id']])['data']['topic_type'];
                $topic = get_curl('topic', 'topic/info', ['id' => $data['topic_id']]);
                if ($topic['status'] === true) {
                    return true;
                } else {
                    return false;
                }
            })
            ->withAttr('topic_type', function ($value, $data) {
                $topic = get_curl('topic', 'topic/info', ['id' => $data['topic_id']]);
                if ($topic['status'] === true) {
                    return get_curl('topic', 'topic/info', ['id' => $data['topic_id']])['data']['topic_type'];
                } else {
                    return false;
                }
            })
            ->withAttr('user', function ($value, $data) {
                return get_curl('user', 'user/get_user_information', ['id' => $data['send_user_id']])['data'];
            })
            ->paginate();
        Db::name('message')
            ->where('receive_user_id', USERID)
            ->setField('status', 1);
        get_curl('user', 'user/edit_message', ['id' => USERID, 'quantity' => 0]);

        $msg['msg'] = '站内信获取成功';
        $msg['list'] = $result;
        return $this->successJson($msg);
    }
}
